﻿using Common.Type;
using System;
using System.Configuration;
using System.Linq;

namespace Authentication.Configuration
{
    public static class SystemConfiguration
    {
        private static int defaultPageSize = 0;
        public static int DefaultPageSize
        {
            get
            {
                if (defaultPageSize > 0) return defaultPageSize;
                int.TryParse(ConfigurationManager.AppSettings["DefaultPageSize"], out defaultPageSize);
                if (defaultPageSize <= 0) defaultPageSize = 20;
                return defaultPageSize;
            }
        }


        private static string connectionString;
        public static string ConnectionString
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(connectionString)) return connectionString;
                connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                if (string.IsNullOrWhiteSpace(connectionString)) return "DefaultConnection";
                return connectionString;
            }
        }


        private static string schema;
        public static string Schema
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(schema)) return schema;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "SCHEMA");
                if (!string.IsNullOrEmpty(key)) schema = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(schema)) return "dbo";
                schema = schema.ToUpper();
                return schema;
            }
        }

        private static string supperAdmin;
        public static string SUPPER_ADMIN
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(supperAdmin)) return supperAdmin;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "SUPPER_ADMIN");
                if (!string.IsNullOrEmpty(key)) supperAdmin = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(supperAdmin)) return "SYSADMIN";
                supperAdmin = supperAdmin.ToUpper();
                return supperAdmin;
            }
        }

        private static string tablePrefix;
        public static string TABLE_PREFIX
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(tablePrefix)) return tablePrefix;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "TABLE_PREFIX");
                if (!string.IsNullOrEmpty(key)) tablePrefix = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(tablePrefix)) return string.Empty;
                tablePrefix = tablePrefix.ToUpper();
                return tablePrefix;
            }
        }

        private static ConvertType? namingType;
        public static ConvertType NamingType
        {
            get
            {
                if (namingType.HasValue) return namingType.Value;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x == "NamingType");
                string stringVal = string.Empty;
                if (!string.IsNullOrEmpty(key)) stringVal = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(stringVal))
                {
                    namingType = ConvertType.CAPITALIZED;
                    return ConvertType.CAPITALIZED;
                }
                stringVal = stringVal.ToUpper();

                ConvertType result = ConvertType.CAPITALIZED;
                if (Enum.TryParse<ConvertType>(stringVal, out result))
                {
                    namingType = result;
                    return result;
                }

                return result;
            }
        }



        private static bool? isSplitColumn;
        public static bool IS_SPLIT_COLUMN_NAME
        {
            get
            {
                if (isSplitColumn.HasValue) return isSplitColumn.Value;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "IS_SPLIT_COLUMN_NAME");
                string stringVal = string.Empty;
                if (!string.IsNullOrEmpty(key)) stringVal = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(stringVal))
                {
                    isSplitColumn = true;
                    return true;
                }

                stringVal = stringVal.Trim().ToUpper();
                if (stringVal == "1" || stringVal == "TRUE")
                {
                    isSplitColumn = true;
                    return true;
                }

                isSplitColumn = false;
                return false;
            }
        }

        private static bool? isSplitTable;
        public static bool IS_SPLIT_TABLE_NAME
        {
            get
            {
                if (isSplitTable.HasValue) return isSplitTable.Value;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "IS_SPLIT_TABLE_NAME");
                string stringVal = string.Empty;
                if (!string.IsNullOrEmpty(key)) stringVal = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(stringVal))
                {
                    isSplitTable = true;
                    return true;
                }

                stringVal = stringVal.Trim().ToUpper();
                if (stringVal == "1" || stringVal == "TRUE")
                {
                    isSplitTable = true;
                    return true;
                }

                isSplitTable = false;
                return false;
            }
        }

        private static Guid applicationKey;
        public static Guid ApplicationKey
        {
            get
            {
                if (applicationKey != Guid.Empty) return applicationKey;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x == "ApplicationKey");

                if (!string.IsNullOrEmpty(key) && Guid.TryParse(ConfigurationManager.AppSettings[key], out applicationKey))
                    return applicationKey;

                return Guid.Empty;
            }
        }

        private static int longCacheTime = 0;
        public static int LongCacheTime
        {
            get
            {
                if (longCacheTime > 0) return longCacheTime;
                if (!int.TryParse(ConfigurationManager.AppSettings["LongCacheTime"], out longCacheTime))
                {
                    longCacheTime = 1440;
                }
                return longCacheTime;
            }
        }


        private static int defaultCacheTime = 0;
        public static int DefaultCacheTime
        {
            get
            {
                if (defaultCacheTime > 0) return defaultCacheTime;
                if (!int.TryParse(ConfigurationManager.AppSettings["DefaultCacheTime"], out defaultCacheTime))
                {
                    defaultCacheTime = 5;
                }
                return defaultCacheTime;
            }
        }

        private static float smallCacheTime = 0;
        public static float SmallCacheTime
        {
            get
            {
                if (smallCacheTime > 0) return smallCacheTime;
                if (!float.TryParse(ConfigurationManager.AppSettings["SmallCacheTime"], out smallCacheTime))
                {
                    smallCacheTime = 0.2f;
                }
                return smallCacheTime;
            }
        }

        private static string registerGroup;
        public static string RegisterGroup
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(registerGroup)) return registerGroup;
                registerGroup = ConfigurationManager.AppSettings["RegisterGroup"];
                if (string.IsNullOrWhiteSpace(registerGroup))
                {
                    registerGroup = "Register";
                }
                return registerGroup;
            }
        }

        private static string deleteRoleAction;
        public static string DeleteRoleAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteRoleAction)) return deleteRoleAction;
                deleteRoleAction = ConfigurationManager.AppSettings["DeleteRoleAction"];
                if (string.IsNullOrWhiteSpace(deleteRoleAction))
                {
                    deleteRoleAction = "/system/delete-role";
                }
                return deleteRoleAction;
            }
        }

        private static string deleteRegionAction;
        public static string DeleteRegionAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteRegionAction)) return deleteRegionAction;
                deleteRegionAction = ConfigurationManager.AppSettings["DeleteRegionAction"];
                if (string.IsNullOrWhiteSpace(deleteRegionAction))
                {
                    deleteRegionAction = "/customer/delete-region";
                }
                return deleteRegionAction;
            }
        }

        private static string deleteIncentiveAdhhocAction;
        public static string DeleteIncentiveAdhhocAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteIncentiveAdhhocAction)) return deleteIncentiveAdhhocAction;
                deleteIncentiveAdhhocAction = ConfigurationManager.AppSettings["DeleteIncentiveAdhhocAction"];
                if (string.IsNullOrWhiteSpace(deleteIncentiveAdhhocAction))
                {
                    deleteIncentiveAdhhocAction = "/customer/delete-incentiveAdhoc";
                }
                return deleteIncentiveAdhhocAction;
            }
        }

        private static string deleteOrganizationAction;
        public static string DeleteOrganizationAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteOrganizationAction)) return deleteOrganizationAction;
                deleteOrganizationAction = ConfigurationManager.AppSettings["DeleteOrganizationAction"];
                if (string.IsNullOrWhiteSpace(deleteOrganizationAction))
                {
                    deleteOrganizationAction = "/customer/delete-organization";
                }
                return deleteOrganizationAction;
            }
        }

        private static string deleteProvinceAction;
        public static string DeleteProvinceAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteProvinceAction)) return deleteProvinceAction;
                deleteProvinceAction = ConfigurationManager.AppSettings["DeleteProvinceAction"];
                if (string.IsNullOrWhiteSpace(deleteProvinceAction))
                {
                    deleteProvinceAction = "/customer/delete-province";
                }
                return deleteProvinceAction;
            }
        }

        private static string deleteDistrictAction;
        public static string DeleteDistrictAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteDistrictAction)) return deleteDistrictAction;
                deleteDistrictAction = ConfigurationManager.AppSettings["DeleteDistrictAction"];
                if (string.IsNullOrWhiteSpace(deleteDistrictAction))
                {
                    deleteDistrictAction = "/customer/delete-district";
                }
                return deleteDistrictAction;
            }
        }


        private static string deleteObjectAction;
        public static string DeleteObjectAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteObjectAction)) return deleteObjectAction;
                deleteObjectAction = ConfigurationManager.AppSettings["DeleteObjectAction"];
                if (string.IsNullOrWhiteSpace(deleteObjectAction))
                {
                    deleteObjectAction = "/system/delete-object";
                }
                return deleteObjectAction;
            }
        }

        private static string assignGroupAction;
        public static string AssignGroupAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(assignGroupAction)) return assignGroupAction;
                assignGroupAction = ConfigurationManager.AppSettings["AssignGroupAction"];
                if (string.IsNullOrWhiteSpace(assignGroupAction))
                {
                    assignGroupAction = "/account/assign-group";
                }
                return assignGroupAction;
            }
        }

        private static string usersInGroupAction;
        public static string UsersInGroupAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(usersInGroupAction)) return usersInGroupAction;
                usersInGroupAction = ConfigurationManager.AppSettings["UsersInGroupAction"];
                if (string.IsNullOrWhiteSpace(usersInGroupAction))
                {
                    usersInGroupAction = "/account/users-in-group";
                }
                return usersInGroupAction;
            }
        }

        private static string deleteGroupAction;
        public static string DeleteGroupAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(deleteGroupAction)) return deleteGroupAction;
                deleteGroupAction = ConfigurationManager.AppSettings["DeleteGroupAction"];
                if (string.IsNullOrWhiteSpace(deleteGroupAction))
                {
                    deleteGroupAction = "/account/delete-group";
                }
                return deleteGroupAction;
            }
        }

        private static string activeUserAction;
        public static string ActiveUserAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(activeUserAction)) return activeUserAction;
                activeUserAction = ConfigurationManager.AppSettings["ActiveUserAction"];
                if (string.IsNullOrWhiteSpace(activeUserAction))
                {
                    activeUserAction = "/account/active-user";
                }
                return activeUserAction;
            }
        }

        private static string activeShopAction;
        public static string ActiveShopAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(activeShopAction)) return activeShopAction;
                activeShopAction = ConfigurationManager.AppSettings["ActiveShopAction"];
                if (string.IsNullOrWhiteSpace(activeShopAction))
                {
                    activeShopAction = "/customer/active-shop";
                }
                return activeShopAction;
            }
        }

        private static string confirmSaleoutAction;
        public static string ConfirmSaleoutAction
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(confirmSaleoutAction)) return confirmSaleoutAction;
                confirmSaleoutAction = ConfigurationManager.AppSettings["ConfirmSaleoutAction"];
                if (string.IsNullOrWhiteSpace(confirmSaleoutAction))
                {
                    confirmSaleoutAction = "/warranty/confirm-saleout";
                }
                return confirmSaleoutAction;
            }
        }

        private static string dateTimeFormat;
        public static string DateTimeFormat
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(dateTimeFormat)) return dateTimeFormat;
                dateTimeFormat = ConfigurationManager.AppSettings["DateTimeFormat"];
                if (string.IsNullOrWhiteSpace(dateTimeFormat))
                {
                    dateTimeFormat = "MM/dd/yyyy";
                }
                return dateTimeFormat;
            }
        }

        /// <summary>
        /// Configuration of time live of OTP
        /// </summary>
        public const double OTPLiveTime = 60d;
        public const double OTPLiveTimeMinute = 10;
        /// <summary>
        /// Configuration of DateTime format
        /// </summary>
        public const string CounterDateTimeFormat = "yyyyMMddHHmmss";


        private static string roleLinkAccess;
        public static string RoleLinkAccess
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(roleLinkAccess)) return roleLinkAccess;
                roleLinkAccess = ConfigurationManager.AppSettings["RoleLinkAccess"];
                if (string.IsNullOrWhiteSpace(roleLinkAccess))
                {
                    roleLinkAccess = "ROLE15";
                }
                return roleLinkAccess;
            }
        }


        private static string roleLinkAccessKey;
        public static string RoleLinkAccessKey
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(roleLinkAccessKey)) return roleLinkAccessKey;
                roleLinkAccessKey = ConfigurationManager.AppSettings["RoleLinkAccessKey"];
                if (string.IsNullOrWhiteSpace(roleLinkAccessKey))
                {
                    roleLinkAccessKey = "ROLE_LINK_ACCESS_KEY";
                }
                return roleLinkAccessKey;
            }
        }
    }
}
